package com.atguigu.tingshu.order.mapper;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {

    /**
     * 根据用户id、付费项目id、付费项目类型查询订单状态
     *
     * @param userId
     * @param itemId
     * @param itemType
     * @return
     */
    @Select("""
            SELECT
            	count(*)\s
            FROM
            	order_info o
            	INNER JOIN order_detail od ON o.id = od.order_id\s
            	AND od.item_id = #{itemId}\s
            	AND o.order_status = '0901'\s
            	AND o.item_type = #{itemType}
            	AND o.user_id = #{userId}
            """)
    Long getUserOrderStatusByUserIdAndItemId(@Param("userId") Long userId, @Param("itemId") Long itemId, @Param("itemType") String itemType);

    @Select("""
            SELECT
            	od.item_id\s
            FROM
            	order_info o
            	INNER JOIN order_detail od ON o.id = od.order_id\s
            	AND o.order_status = '0901'\s
            	AND o.item_type = #{itemType}
            	AND o.user_id = #{userId}
            """)
    List<Long> getUserOrderStatusByUserIdAndItemIdTrack(Long userId, String itemType);

    @Update("""
            UPDATE order_info\s
            SET order_info.order_status = '0902'\s
            WHERE
            	order_info.user_id = #{userId}\s
            	AND order_info.order_no = #{orderNo}\s
            	AND order_info.order_status = '0901'
            """)
    int updateOrderStatus(@Param("orderNo") String orderNo, @Param("userId") Long userId);

    @Update("""
            UPDATE order_info\s
            SET order_info.order_status = '0903'\s
            WHERE
            	order_info.order_no = #{orderNo}\s
            	AND order_info.order_status = '0901'
            """)
    int cancelOrder(String orderNo);
}
